package com.sc.controller;

import com.github.pagehelper.PageInfo;
import com.sc.pojo.*;
import com.sc.pojo.Class;
import com.sc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    ExamService es;
    @Autowired
    ExamSheetService ss;
    @Autowired
    TitleTypeService tts;
    @Autowired
    TitleAdminService tas;
    @Autowired
    ClassService cs;//班级
    @RequestMapping("/toIndex")
    public String index(Map m){
        List<Titletype> titletypes = tts.showAll();
        m.put("titleTypes",titletypes);
        return "/exammodol/index";
    }
    //用户考试历史界面
    @RequestMapping("/toHistory")
    public String toHistory(@RequestParam(defaultValue = "1")Integer pageNum, Map m,
                            String name, @RequestParam(defaultValue ="0") Integer corel,
                            @RequestParam(defaultValue = "100") Integer corer, String subject, HttpSession session){
        Integer userId = ((User) session.getAttribute("theUser")).getId();
        PageInfo<Exam> exam=es.showHistory(pageNum,4,name,corel,corer,subject,userId);
         m.put("exam",exam);
         if (name!=null){
             m.put("name",name);
         }
        if (corel!=null){
            m.put("corel",corel);
        }
        if (corer!=null){
            m.put("corer",corer);
        }
        if (subject!=null){
            m.put("subject",subject);
        }
        return "/exammodol/examhis";
    }
    @RequestMapping("/addExamSheet")
    public String  addExamSheet(Integer userid, long examid,Integer[] titleids,String[] answer ,HttpServletRequest request,Map m){
        //防止重考先删除之前考的
        ss.deleteExamSheet(examid);
        //得到用户答案集合
        String[] userResult = new String[titleids.length];
        for (int i = 0; i <userResult.length ; i++) {
            userResult[i] = request.getParameter(titleids[i]+"");
        }
        ss. addExamSheet(userid,examid,titleids,answer,userResult);
        int sum = 0;
        for (int i = 0; i <answer.length ; i++) {
            if(answer[i].equalsIgnoreCase(userResult[i])) sum +=100/titleids.length;
            else  sum+= 0;
        }
        m.put("score",sum);
         return "/exammodol/after/resultScore";
    }

    @RequestMapping(value = "/addExamSheet1")
    @ResponseBody
    public Integer addExamSheet1(Integer userid, long examid,Integer[] titleids,String[] answer ,HttpServletRequest request){
        //得到用户答案集合
        System.out.println(userid);
        System.out.println(examid);
        System.out.println(Arrays.toString(titleids));
        System.out.println(Arrays.toString(answer));
        String[] userResult = new String[titleids.length];
        for (int i = 0; i <userResult.length ; i++) {
            userResult[i] = request.getParameter(titleids[i]+"");
            System.out.println(userResult[i]);
        }
        ss. addExamSheet(userid,examid,titleids,answer,userResult);
        int sum = 0;
        for (int i = 0; i <answer.length ; i++) {
            if(answer[i].equalsIgnoreCase(userResult[i])) sum += 100/titleids.length;
            else  sum+= 0;
        }
        return sum;
    }

    //根据班级查询所有所在班级的试卷
    @RequestMapping("/classExam")
    public String classExam(@RequestParam(defaultValue = "1")Integer pageNum,Integer classid,String subject,Map m){
        PageInfo<Exam> exams = es.classExam(pageNum,3,classid,subject);
        if(subject != null) m.put("subject",subject);
        m.put("p",exams);
        return "/exammodol/after/myExam";

    }

    //试卷管理的展示
    @RequestMapping("/examShow")
    public String examShow(@RequestParam(defaultValue = "1")Integer pageNum, Map m,
                           String name,String subject){
        PageInfo<Exam> examPageInfo = es.examShow(pageNum, 5,name,subject);
        m.put("p",examPageInfo);
        if (name!=null){
            m.put("name",name);
        }
        if (subject!=null){
            m.put("subject",subject);
        }
        return "/exammodol/afterExam/list";
    }
    //删除多个试卷
    @RequestMapping("/deleteAllExam")
    public String deleteAllExam(Long[] ids){
        if (ids != null) {
            for(Long id:ids){
                //班级考试记录表
                ss.deleteClassExam(id);
                //考试记录表中移除
                tas.deleteExamDetail(id);
                //考试详情表中移除
                ss.deleteExamSheet(id);
            }
            es.deleteAllExam(ids);
        }
        return "redirect:/exam/examShow";
    }
    //自我测试生成题目
    @RequestMapping("/myExamTest")
    public String myExamTest(Long examid,Map m){
        List<Title> titles = tas.myExamTest(examid);
        Exam exam = es.showOne(examid);
        if(titles.size() == 0) return "/exammodol/404";
        m.put("titles",titles);
        m.put("exam",exam);
        return "/exammodol/after/myExamTest";
    }

    //删除后台考试历史记录
    @RequestMapping("/toDeleteHistory")
    public String toDeleteHistory(long id){
        ss.deleteExamSheet(id);
//        es.deleteHistory(id);
        return "redirect:/exam/toAfterHistoryBack";
    }
    //删除单个试卷
    @RequestMapping("/toDeleteExam")
    public String totoDeleteExam(long id){
        //班级考试记录表
        ss.deleteClassExam(id);
        //考试记录表中移除
        tas.deleteExamDetail(id);
        //考试详情表中移除
        ss.deleteExamSheet(id);
        es.deleteHistory(id);
        return "redirect:/exam/examShow";
    }
    //新增试卷
    @RequestMapping("/addExam")
    public String addExam(Exam exam,String username,Integer classid){
        System.out.println(classid);
        long id = new Date().getTime();
        String  name = username + id;
        exam.setId(id);
        exam.setName(name);
        exam.setBegintime(new Date());
        exam.setTimelength(1);
        exam.setAllscore(100.0);
        exam.setChoicescore(100.0);
        List<Title> titles = tas.addTitle(exam.getChoicenum());
        if(titles.size() == 0) return "/exammodol/404";
        //添加班级id和试卷id
        es.addClassExam(exam.getId(),classid);
        //添加考试详情 exam_detail
        tas.addExamInfo(titles,exam.getId());
        es.addTitleExam(exam);
        return "redirect:/exam/examShow";
    }

    //去新增试卷页面
    @RequestMapping("toAddExam")
    public String toAddExam(Map m){
        List<Titletype> titletypes = tts.showAll();
        List<Class> classes = cs.queryAll();
        m.put("titleType",titletypes);
        m.put("classes",classes);
        return "/exammodol/afterExam/add";
    }
    //去后台考试历史页面
    @RequestMapping("/toAfterHistoryBack")
    public String toAfterHistoryBack(@RequestParam(defaultValue = "1")Integer pageNum ,Map m,
                                 String name,@RequestParam(defaultValue ="0") Integer corel,
                                 @RequestParam(defaultValue = "100") Integer corer,String subject){

        PageInfo<Exam> examback=es.showHistoryBack(pageNum,4,name,corel,corer,subject);
        m.put("examback",examback);
        if (name!=null){
            m.put("name",name);
        }
        if (corel!=null){
            m.put("corel",corel);
        }
        if (corer!=null){
            m.put("corer",corer);
        }
        if (subject!=null){
            m.put("subject",subject);
        }
        return "/exammodol/afterHistory/list";

    }
    //下载试题
    @RequestMapping("/downLoadExamInfo")
    public String downLoadExamInfo(long examid,Integer userid,Map m){
        List<ExamSheet> show = ss.show(examid, userid);
        m.put("examSheet",show);
        return "/exammodol/downLoadExamInfo";
    }
    //后台下载试题
    @RequestMapping("/downLoadExam")
    public String downLoadExam(long examid,Integer userid,Map m){
        List<ExamSheet> show = ss.show(examid, userid);
        m.put("examSheet",show);
        return "/exammodol/afterHistory/downLoadExam";
    }

    //查看前台考试历史试题
    @RequestMapping("/toshowExamInfo")
    public String toshowExamInfo(long examid,Integer userid,Map m){
        List<ExamSheet> show = ss.show(examid, userid);
        if(show == null||show.size() == 0) return "/exammodol/404";
        int num = 0;
        for (ExamSheet examSheet:show) {
            if (examSheet.getTitle() == null) num+=0;
            else num+=1;
        }
        if (num == 0) return "/exammodol/404";

        m.put("examSheet",show);
        return "/exammodol/examInfo";
    }
    //查看每张试卷的试题
    @RequestMapping("/showExam")
    public String showExam(long examid,Map m){
        Exam exam = es.showOne(examid);
        List<Title> titles = tas.myExamTest(examid);
        if(titles.size() == 0) return "/exammodol/404";
        m.put("exam",exam);
        m.put("titles",titles);
        return "/exammodol/afterExam/oneExamInfo";
    }
    //后台考试历史展示
    @RequestMapping("/toshowExamInfoBack")
    public String toshowExamInfoBack(long examid,Integer userid,Map m){
        List<ExamSheet> showinfo=ss.show(examid, userid);
        if(showinfo == null||showinfo.size() == 0) return "/exammodol/404";
        int num = 0;
        for (ExamSheet examSheet:showinfo) {
            if (examSheet.getTitle() == null) num+=0;
            else num+=1;
        }
        if (num == 0) return "/exammodol/404";
        m.put("examSheetBack",showinfo);
        return "/exammodol/afterHistory/info";
    }


    @RequestMapping("/toLevel")
    public String toLevel(Integer typeId,Map m){
        m.put("typeId",typeId);
        Titletype titletype = tts.showOne(typeId);
        m.put("titleType",titletype);
        return "/exammodol/level" ;
    }
    @RequestMapping("/toExit")
    public String toExit() {return "redirect:/home.jsp"; }

}
